using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class StockType
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("StockTypeID", "HLJXC_StockType");
		}
		public bool Exists(int StockTypeID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_StockType");
			stringBuilder.Append(" where StockTypeID=@StockTypeID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockTypeID", SqlDbType.Int, 4)
			};
			array[0].Value = StockTypeID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.StockType model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_StockType(");
			stringBuilder.Append("AccountSubjectID,StockTypeNumber,StockTypeName,StockTypeSpell,StockTypeStatue,Remark)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@AccountSubjectID,@StockTypeNumber,@StockTypeName,@StockTypeSpell,@StockTypeStatue,@Remark)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4),
				new SqlParameter("@StockTypeNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeName", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeSpell", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeStatue", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255)
			};
			array[0].Value = model.AccountSubjectID;
			array[1].Value = model.StockTypeNumber;
			array[2].Value = model.StockTypeName;
			array[3].Value = model.StockTypeSpell;
			array[4].Value = model.StockTypeStatue;
			array[5].Value = model.Remark;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.StockType model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_StockType set ");
			stringBuilder.Append("AccountSubjectID=@AccountSubjectID,");
			stringBuilder.Append("StockTypeNumber=@StockTypeNumber,");
			stringBuilder.Append("StockTypeName=@StockTypeName,");
			stringBuilder.Append("StockTypeSpell=@StockTypeSpell,");
			stringBuilder.Append("StockTypeStatue=@StockTypeStatue,");
			stringBuilder.Append("Remark=@Remark");
			stringBuilder.Append(" where StockTypeID=@StockTypeID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@AccountSubjectID", SqlDbType.Int, 4),
				new SqlParameter("@StockTypeNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeName", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeSpell", SqlDbType.NVarChar, 50),
				new SqlParameter("@StockTypeStatue", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255),
				new SqlParameter("@StockTypeID", SqlDbType.Int, 4)
			};
			array[0].Value = model.AccountSubjectID;
			array[1].Value = model.StockTypeNumber;
			array[2].Value = model.StockTypeName;
			array[3].Value = model.StockTypeSpell;
			array[4].Value = model.StockTypeStatue;
			array[5].Value = model.Remark;
			array[6].Value = model.StockTypeID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int StockTypeID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_StockType ");
			stringBuilder.Append(" where StockTypeID=@StockTypeID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockTypeID", SqlDbType.Int, 4)
			};
			array[0].Value = StockTypeID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string StockTypeIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_StockType ");
			stringBuilder.Append(" where StockTypeID in (" + StockTypeIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.StockType GetModel(int StockTypeID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 StockTypeID,AccountSubjectID,StockTypeNumber,StockTypeName,StockTypeSpell,StockTypeStatue,Remark from HLJXC_StockType ");
			stringBuilder.Append(" where StockTypeID=@StockTypeID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@StockTypeID", SqlDbType.Int, 4)
			};
			array[0].Value = StockTypeID;
			HKB.Data.Model.StockType stockType = new HKB.Data.Model.StockType();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.StockType result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.StockType DataRowToModel(DataRow row)
		{
			HKB.Data.Model.StockType stockType = new HKB.Data.Model.StockType();
			if (row != null)
			{
				if (row["StockTypeID"] != null && row["StockTypeID"].ToString() != "")
				{
					stockType.StockTypeID = int.Parse(row["StockTypeID"].ToString());
				}
				if (row["AccountSubjectID"] != null && row["AccountSubjectID"].ToString() != "")
				{
					stockType.AccountSubjectID = new int?(int.Parse(row["AccountSubjectID"].ToString()));
				}
				if (row["StockTypeNumber"] != null)
				{
					stockType.StockTypeNumber = row["StockTypeNumber"].ToString();
				}
				if (row["StockTypeName"] != null)
				{
					stockType.StockTypeName = row["StockTypeName"].ToString();
				}
				if (row["StockTypeSpell"] != null)
				{
					stockType.StockTypeSpell = row["StockTypeSpell"].ToString();
				}
				if (row["StockTypeStatue"] != null)
				{
					stockType.StockTypeStatue = row["StockTypeStatue"].ToString();
				}
				if (row["Remark"] != null)
				{
					stockType.Remark = row["Remark"].ToString();
				}
			}
			return stockType;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select StockTypeID,AccountSubjectID,StockTypeNumber,StockTypeName,StockTypeSpell,StockTypeStatue,Remark ");
			stringBuilder.Append(" FROM HLJXC_StockType ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" StockTypeID,AccountSubjectID,StockTypeNumber,StockTypeName,StockTypeSpell,StockTypeStatue,Remark ");
			stringBuilder.Append(" FROM HLJXC_StockType ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_StockType ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.StockTypeID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_StockType T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
